from manimlib import *

sys.path.append('.')
from customized import *


class p1(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(UP * 2 + RIGHT * 0.5)
        cd = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(DOWN * 2 + LEFT * 0.5)
        a, b, c, d = ab.get_left(), ab.get_right(), cd.get_right(), cd.get_left()
        e, f, g, h = ab.get_center(), (b + c) / 2, cd.get_center(), (a + d) / 2
        af, ag, bg, bh, = CustomizedLine(a, f), CustomizedLine(a, g), CustomizedLine(b, g), CustomizedLine(b, h)
        ch, ce, de, df = CustomizedLine(c, h), CustomizedLine(c, e), CustomizedLine(d, e), CustomizedLine(d, f)
        self.add(ab, cd, af, ag, bg, bh, ch, ce, de, df,
                 CustomizedLine(b, c),
                 CustomizedLine(a, d),
                 CustomizedTex('A').next_to(a, UL),
                 CustomizedTex('B').next_to(b, UR),
                 CustomizedTex('C').next_to(c, DR),
                 CustomizedTex('D').next_to(d, DL),
                 CustomizedTex('E').next_to(e, UP),
                 CustomizedTex('F').next_to(f),
                 CustomizedTex('G').next_to(g, DOWN),
                 CustomizedTex('H').next_to(h, LEFT)
                 )
        # 八边形
        o = ORIGIN
        o1, o3, o5, o7 = (e + o) / 2, (o + f) / 2, (o + g) / 2, (o + h) / 2
        o2, o4, o6, o8 = (o + b) / 3, (o + c) / 3, (o + d) / 3, (o + a) / 3
        self.add(CustomizedPolygon(o1, o2, o3, o4, o5, o6, o7, o8).set_fill(LV, 1))


class p2(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(UP * 2 + RIGHT * 0.5)
        cd = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(DOWN * 2 + LEFT * 0.5)
        a, b, c, d = ab.get_left(), ab.get_right(), cd.get_right(), cd.get_left()
        e, f, g, h = ab.get_center(), (b + c) / 2, cd.get_center(), (a + d) / 2
        af, ag, bg, bh, = CustomizedLine(a, f), CustomizedLine(a, g), CustomizedLine(b, g), CustomizedLine(b, h)
        ch, ce, de, df = CustomizedLine(c, h), CustomizedLine(c, e), CustomizedLine(d, e), CustomizedLine(d, f)
        self.add(ab, cd, af, ag, bg, bh, ch, ce, de, df,
                 CustomizedLine(b, c),
                 CustomizedLine(a, d),
                 CustomizedTex('A').next_to(a, UL),
                 CustomizedTex('B').next_to(b, UR),
                 CustomizedTex('C').next_to(c, DR),
                 CustomizedTex('D').next_to(d, DL),
                 CustomizedTex('E').next_to(e, UP),
                 CustomizedTex('F').next_to(f),
                 CustomizedTex('G').next_to(g, DOWN),
                 CustomizedTex('H').next_to(h, LEFT)
                 )
        # 八边形
        o = ORIGIN
        o1, o3, o5, o7 = (e + o) / 2, (o + f) / 2, (o + g) / 2, (o + h) / 2
        o2, o4, o6, o8 = (o + b) / 3, (o + c) / 3, (o + d) / 3, (o + a) / 3
        self.add(CustomizedPolygon(o1, o2, o3, o4, o5, o6, o7, o8).set_fill(LV, 1))
        # p2
        eg, fh = CustomizedLine(e, g), CustomizedLine(f, h)
        self.add(eg, fh, CustomizedTex('O').next_to(o, DR))


class p3(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())

        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(UP * 2 + RIGHT * 0.5)
        cd = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(DOWN * 2 + LEFT * 0.5)
        a, b, c, d = ab.get_left(), ab.get_right(), cd.get_right(), cd.get_left()
        e, f, g, h = ab.get_center(), (b + c) / 2, cd.get_center(), (a + d) / 2
        af, ag, bg, bh, = CustomizedLine(a, f), CustomizedLine(a, g), CustomizedLine(b, g), CustomizedLine(b, h)
        ch, ce, de, df = CustomizedLine(c, h), CustomizedLine(c, e), CustomizedLine(d, e), CustomizedLine(d, f)

        self.add(ab, cd, af, ag, bg, bh, ch, ce, de, df,
                 CustomizedLine(b, c).set_stroke(width=2),
                 CustomizedLine(a, d).set_stroke(width=2),
                 CustomizedTex('A').next_to(a, UL),
                 CustomizedTex('B').next_to(b, UR),
                 CustomizedTex('C').next_to(c, DR),
                 CustomizedTex('D').next_to(d, DL),
                 CustomizedTex('E').next_to(e, UP),
                 CustomizedTex('F').next_to(f),
                 CustomizedTex('G').next_to(g, DOWN),
                 CustomizedTex('H').next_to(h, LEFT)
                 )
        # 八边形
        o = ORIGIN
        o1, o3, o5, o7 = (e + o) / 2, (o + f) / 2, (o + g) / 2, (o + h) / 2
        o2, o4, o6, o8 = (o + b) / 3, (o + c) / 3, (o + d) / 3, (o + a) / 3
        self.add(CustomizedPolygon(o1, o2, o3, o4, o5, o6, o7, o8).set_fill(LV, 1).set_stroke(width=2))
        # p2
        eg, fh = CustomizedLine(e, g), CustomizedLine(f, h)
        VGroup(ab, cd, af, ag, bg, bh, ch, ce, de, df, eg, fh).set_stroke(width=2)
        self.add(eg, fh)
        # p3
        self.camera.frame.scale(0.5).move_to((e + f) / 2)
        mid_ef = (e + f) / 2
        self.add(
            CustomizedTex('O').next_to(o, DR),
            CustomizedLine(o1, (b + f) / 2).set_stroke(width=2).set_color(JH),
            CustomizedLine(o3, (b + e) / 2).set_stroke(width=2).set_color(JH),
            CustomizedLine((o + o1) / 2, (o3 + mid_ef) / 2).set_stroke(width=2).set_color(RED),
            CustomizedLine((o + o3) / 2, (o1 + mid_ef) / 2).set_stroke(width=2).set_color(RED)
        )


class p4(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())

        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(UP * 2 + RIGHT * 0.5)
        cd = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(DOWN * 2 + LEFT * 0.5)
        a, b, c, d = ab.get_left(), ab.get_right(), cd.get_right(), cd.get_left()
        e, f, g, h = ab.get_center(), (b + c) / 2, cd.get_center(), (a + d) / 2
        af, ag, bg, bh, = CustomizedLine(a, f), CustomizedLine(a, g), CustomizedLine(b, g), CustomizedLine(b, h)
        ch, ce, de, df = CustomizedLine(c, h), CustomizedLine(c, e), CustomizedLine(d, e), CustomizedLine(d, f)
        self.add(ab, cd, af, ag, bg, bh, ch, ce, de, df,
                 CustomizedLine(b, c),
                 CustomizedLine(a, d),
                 CustomizedTex('A').next_to(a, UL),
                 CustomizedTex('B').next_to(b, UR),
                 CustomizedTex('C').next_to(c, DR),
                 CustomizedTex('D').next_to(d, DL),
                 CustomizedTex('E').next_to(e, UP),
                 CustomizedTex('F').next_to(f),
                 CustomizedTex('G').next_to(g, DOWN),
                 CustomizedTex('H').next_to(h, LEFT)
                 )
        # 八边形
        o = ORIGIN
        o1, o3, o5, o7 = (e + o) / 2, (o + f) / 2, (o + g) / 2, (o + h) / 2
        o2, o4, o6, o8 = (o + b) / 3, (o + c) / 3, (o + d) / 3, (o + a) / 3
        self.add(CustomizedPolygon(o1, o2, o3, o4, o5, o6, o7, o8).set_stroke(width=1).set_fill(LV, 1))
        # p2
        eg, fh = CustomizedLine(e, g), CustomizedLine(f, h)
        self.add(eg, fh)
        # p3
        self.camera.frame.scale(0.5).move_to((e + f) / 2)
        mid_ef = (e + f) / 2
        self.add(
            CustomizedTex('O').next_to(o, DR).scale(1/4),
            CustomizedLine(o1, (b + f) / 2).set_color(JH).set_stroke(width=1),
            CustomizedLine(o3, (b + e) / 2).set_color(JH).set_stroke(width=1),
            CustomizedLine((o + o1) / 2, (o3 + mid_ef) / 2).set_stroke(width=1).set_color(RED),
            CustomizedLine((o + o3) / 2, (o1 + mid_ef) / 2).set_stroke(width=1).set_color(RED)
        )
        # p4
        self.camera.frame.scale(0.5).move_to((mid_ef + o) / 2)
        VGroup(ab, cd, af, ag, bg, bh, ch, ce, de, df, eg, fh).set_stroke(width=1)
        self.add(
            CustomizedTex(r'\frac{p}{64}').scale(1/4).shift(UR / 4 + RIGHT / 8),
            CustomizedTex(r'\frac{3}{4}\cdot\frac{p}{64}').scale(1/4).shift(UP / 4 + RIGHT * 0.9),
            CustomizedTex(r'\frac{3}{4}\cdot\frac{p}{64}').scale(1/4).shift(UP * 0.7 + RIGHT / 2)
        )


class p5(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(UP * 2 + RIGHT * 0.5)
        cd = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(DOWN * 2 + LEFT * 0.5)
        a, b, c, d = ab.get_left(), ab.get_right(), cd.get_right(), cd.get_left()
        e, f, g, h = ab.get_center(), (b + c) / 2, cd.get_center(), (a + d) / 2
        af, ag, bg, bh, ch, ce, de, df = CustomizedLine(a, f), CustomizedLine(a, g), CustomizedLine(b,
                                                                                                    g), CustomizedLine(
            b, h), CustomizedLine(c, h), CustomizedLine(c, e), CustomizedLine(d,
                                                                              e), CustomizedLine(
            d, f)
        self.add(ab, cd, af, bg, ch, de,
                 CustomizedLine(b, c),
                 CustomizedLine(a, d),
                 CustomizedTex('A').next_to(a, UL),
                 CustomizedTex('B').next_to(b, UR),
                 CustomizedTex('C').next_to(c, DR),
                 CustomizedTex('D').next_to(d, DL),
                 CustomizedTex('E').next_to(e, UP),
                 CustomizedTex('F').next_to(f),
                 CustomizedTex('G').next_to(g, DOWN),
                 CustomizedTex('H').next_to(h, LEFT)
                 )
        # 八边形
        o = ORIGIN
        self.add(
            CustomizedPolygon(af.point_from_proportion(2 / 5), bg.point_from_proportion(2 / 5),
                              ch.point_from_proportion(2 / 5),
                              de.point_from_proportion(2 / 5)).set_fill(LV, 1))


class p6(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        ab = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(UP * 2 + RIGHT * 0.5)
        cd = CustomizedLine(LEFT * 2.5, RIGHT * 2.5).shift(DOWN * 2 + LEFT * 0.5)
        a, b, c, d = ab.get_left(), ab.get_right(), cd.get_right(), cd.get_left()
        e, f, g, h = ab.get_center(), (b + c) / 2, cd.get_center(), (a + d) / 2
        af, ag, bg, bh, ch, ce, de, df = CustomizedLine(a, f), CustomizedLine(a, g), CustomizedLine(b,
                                                                                                    g), CustomizedLine(
            b, h), CustomizedLine(c, h), CustomizedLine(c, e), CustomizedLine(d,
                                                                              e), CustomizedLine(
            d, f)
        self.add(ab, cd, af, bg, ch, de,
                 CustomizedLine(b, c),
                 CustomizedLine(a, d),
                 CustomizedTex('A').next_to(a, UL),
                 CustomizedTex('B').next_to(b, UR),
                 CustomizedTex('C').next_to(c, DR),
                 CustomizedTex('D').next_to(d, DL),
                 CustomizedTex('E').next_to(e, UP),
                 CustomizedTex('F').next_to(f),
                 CustomizedTex('G').next_to(g, DOWN),
                 CustomizedTex('H').next_to(h, LEFT)
                 )
        # 八边形
        o = ORIGIN
        m, n, = af.point_from_proportion(2 / 5), bg.point_from_proportion(2 / 5)
        p, q = ch.point_from_proportion(2 / 5), de.point_from_proportion(2 / 5)
        self.add(CustomizedPolygon(m, n, p, q).set_fill(LV, 1))
        # p6
        qdh = CustomizedPolygon(q, d, h).set_fill(JH, 1)
        self.add(
            qdh,
            CustomizedPolygon(a, m, q, h).set_fill(JH, 1)
        )
        self.play(Rotate(qdh, PI,axis=[0,0,-1],about_point=h))
        self.wait()
